package com.guandi.geekbrowser.primary.service.impl;

import com.guandi.geekbrowser.primary.entity.TaskItem;
import com.guandi.geekbrowser.primary.repository.TaskItemRepository;
import com.guandi.geekbrowser.primary.service.TaskItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.List;

@Service
public class TaskItemImpl implements TaskItemService {

    @Autowired
    private TaskItemRepository taskItemRepository;

    @Override
    public void save(TaskItem taskItem) {
        taskItemRepository.save(taskItem);
    }

    @Override
    public List<TaskItem> findAllByUserIdAndType(String userId, Integer type) {
        return taskItemRepository.findAllByUserIdAndType(userId,type);
    }

    @Override
    public Integer updateLikedNotesByIdAndUserId(String notes, Long id, String userId) {
        return taskItemRepository.updateLikedNotesByIdAndUserId(notes, id, userId);
    }

    @Override
    public Integer updateLikedByIdAndUserId(String url, String asinCode, Integer totalCount, Long id, String userId) {
        return taskItemRepository.updateLikedByIdAndUserId(url, asinCode, totalCount, id, userId);
    }

    @Override
    public Integer deleteByIdAndUserId(Long id, String userId) {
        return taskItemRepository.deleteByIdAndUserId(id, userId);
    }

    @Override
    public void saveAll(List<TaskItem> taskItems) {
        taskItemRepository.saveAll(taskItems);
    }

    @Override
    public List<TaskItem> findAllByUserIdAndTypeAndTaskName(String userId, Integer type, String taskName) {
        return taskItemRepository.findAllByUserIdAndTypeAndTaskName(userId, type, taskName);
    }

    @Override
    public Page<TaskItem> findAllByUserIdAndTypeAndTaskNamePage(String userId, Integer taskType, String taskName, Integer pageNumber, Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNumber-1, pageSize);
        Specification<TaskItem> spe = new Specification<TaskItem>() {
            @Override
            public Predicate toPredicate(Root<TaskItem> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<Integer> type = root.get("type");
                Path<String> id = root.get("userId");
                Path<String> name = root.get("taskName");
                Predicate idPre = criteriaBuilder.equal(id, userId);
                Predicate typePre = criteriaBuilder.equal(type, taskType);
                Predicate namePre = criteriaBuilder.equal(name, taskName);
                Predicate predicate = criteriaBuilder.and(namePre, idPre,typePre);
                return predicate;
            }
        };
        return taskItemRepository.findAll(spe,pageable);


    }

    @Override
    public List<TaskItem> findAllByUserIdAndTypeAndTaskNameAndFinish(String userId, Integer type, String taskName, Boolean finish) {
        return taskItemRepository.findAllByUserIdAndTypeAndTaskNameAndFinish(userId, type, taskName, finish);
    }

    @Override
    public List<TaskItem> findAllByUserIdAndTypeAndTaskNameAndFinishAndCountry(String userId, Integer type, String taskName, Boolean finish, String country) {
        return taskItemRepository.findAllByUserIdAndTypeAndTaskNameAndFinishAndCountry(userId, type, taskName, finish, country);
    }

    @Override
    public TaskItem findAllByUserIdAndTypeAndId(String userId, Integer type, Long id) {
        return taskItemRepository.findAllByUserIdAndTypeAndId(userId, type, id);
    }

}
